import { message } from 'antd';

import api from '../../api';

export const PenetrateBasicInfoNamespace = 'PenetrateBasicInfoM';
export default {
  namespace: PenetrateBasicInfoNamespace,
  state: {
    PositionsColumns: [], // 产品持仓Columns
    PositionsDataSource: [], // 产品持仓DataSources
  },
  effects: {
    // 获取产品持仓
    *getProductPositionsFn({ payload }, { call, put }) {
      try {
        const response = yield call(api.penetrateBasicInfo.getProductPositions, payload);
        if (response && response.code == 0) {
          yield put({
            type: 'save',
            payload: {
              PositionsDataSource: response.data.dataSource,
              PositionsColumns: response.data.columns,
            },
          });
        } else {
          yield put({
            type: 'save',
            payload: {
              PositionsDataSource: [],
              PositionsColumns: [],
            },
          });
          message.error(response?.msg || '获取产品持仓数据失败！');
        }
      } catch (e) {
        console.log(e);
      }
    },
  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
